package com.shinobi.timetracker.database;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.shinobi.timetracker.settings.Settings;

/**
 * Created by IntelliJ IDEA.
 * User: Semy
 * Date: 09.05.11
 * Time: 22:25
 * To change this template use File | Settings | File Templates.
 */
public class GlobalState extends Application {

    private TimerResult currentTimer = null;
    private CurrentDateSettings currentDate = null;
        
    public CurrentDateSettings getCurrentDate() {
    	
    	if (currentDate == null) {
    		 currentDate = new CurrentDateSettings();
    	}
    	
		return currentDate;
	}

	public void StartTimer(String tag) {
        this.currentTimer = new TimerResult(tag);
        long start = System.currentTimeMillis();
        this.currentTimer.setStartTime(start);
    }

    public TimerResult StopTimer(Context ctx) throws NullPointerException {

        long stop = System.currentTimeMillis();

        if (this.currentTimer == null) {
            throw new NullPointerException("Current Timer null");
        }

        this.currentTimer.setStopTime(stop);

        SaveResult(ctx, currentTimer);

        return currentTimer;
    }

    public void SaveResult(Context ctx, TimerResult result) {

        //TODO:
        try {
			TimeResultProvider.AddTimerResult(ctx, result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("SaveResult", "Added timerResult error??");
		}
    }

    public void AddTag(Context ctx, String tag) {

        //TODO:
        TagsProvider.AddTag(ctx, tag);
    }

    public void RemoveTag(Context ctx, String tag) {
        //TODO:
        //TagsProvider.RemoveTag(ctx, tag);
    }

    public String CurrentTag() {
    	
        SharedPreferences settings = getSharedPreferences(Settings.PREFERENCES, 0);
        String tag = settings.getString(Settings.CURRENT_TAG, "");
        return tag;
    }

    public void SetCurrentTag(String tag) {

        SharedPreferences settings = getSharedPreferences(Settings.PREFERENCES, 0);
        Editor editor = settings.edit();
        editor.putString(Settings.CURRENT_TAG, tag);
        editor.commit();
    }
    
    public ArrayList<String> GetTags(Context ctx) {
    	String[] tags = null;
    	try {
			tags = TagsProvider.GetAllTags(ctx);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return new ArrayList<String>(Arrays.asList(tags));
    }
}
